set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR MICROBLAZE)

if(MINGW OR CYGWIN OR WIN32)
    set(UTIL_SEARCH_CMD where)
elseif(UNIX OR APPLE)
    set(UTIL_SEARCH_CMD which)
endif()

set(TOOLCHAIN_PREFIX mb-)

execute_process(
  COMMAND ${UTIL_SEARCH_CMD} ${TOOLCHAIN_PREFIX}gcc
  OUTPUT_VARIABLE BINUTILS_PATH
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

get_filename_component(XIL_TOOLCHAIN_DIR ${BINUTILS_PATH} DIRECTORY)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc)
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++)

set(CMAKE_OBJCOPY ${XIL_TOOLCHAIN_DIR}/${TOOLCHAIN_PREFIX}objcopy CACHE INTERNAL "objcopy tool")
set(CMAKE_SIZE_UTIL ${XIL_TOOLCHAIN_DIR}/${TOOLCHAIN_PREFIX}size CACHE INTERNAL "size tool")

set(CMAKE_FIND_ROOT_PATH ${BINUTILS_PATH})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

add_compile_options(
  "-Wall"
  "-I${XIL_STANDALONE_BSP_INCLUDE}"
  "-fmessage-length=0"
  "-mno-xl-reorder"
  "-mlittle-endian"
  "-mxl-barrel-shift"
  "-mxl-pattern-compare"
  "-mcpu=v11.0"
  "-mno-xl-soft-mul"
  "-mhard-float"
  "-mno-xl-soft-div"
  "-Wl,--no-relax"
  "-ffunction-sections"
  "-fdata-sections"
  "-MMD"
  "-MP"
)

add_link_options(
  "-L${XIL_STANDALONE_BSP_LIB}"
  "-Wl,-T"
  "-Wl,${CMAKE_CURRENT_LIST_DIR}/lscript.ld"
  "-mlittle-endian"
  "-mxl-barrel-shift"
  "-mxl-pattern-compare"
  "-mcpu=v11.0"
  "-mno-xl-soft-mul"
  "-mhard-float"
  "-mno-xl-soft-div"
  "-Wl,-no-relax"
  "-Wl,--gc-sections"
  "-Wl,--start-group,-lxil,-lgcc,-lc,-lstdc++,--end-group"
)
